package com.bbs.model.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bbs.model.dao.AdminMapper;
import com.bbs.model.dao.UserinfoMapper;
import com.bbs.model.entity.Admin;
import com.bbs.model.entity.AdminExample;
import com.bbs.model.entity.Userinfo;
import com.bbs.model.entity.UserinfoExample;
import com.bbs.model.entity.UserinfoExample.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 管理员管理
 * 
 * @author 乔娇
 *
 */
@Service
public class AdminManageService {

	@Autowired
	private AdminMapper am;
	@Autowired
	private UserinfoMapper um;

	public Admin checklogin(Admin admin) {
		AdminExample example = new AdminExample();
		AdminExample.Criteria cc = example.createCriteria();

		cc.andAdminnameEqualTo(admin.getAdminname());
		cc.andAdminpwdEqualTo(admin.getAdminpwd());

		List<Admin> list = am.selectByExample(example);

		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 极简用户分页查询
	 * 
	 * @param cond
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<Userinfo> searchUserinfo(Userinfo cond, int pageNum, int pageSize) {
		UserinfoExample example = new UserinfoExample();
		Criteria cc = example.createCriteria();

		String username = cond.getUsername();
		if (null != username && !"".equals(username)) {
			cc.andUsernameEqualTo(username);
		}

		PageHelper.startPage(pageNum, pageSize);

		List<Userinfo> list = um.selectByExample(example);

		return new PageInfo<Userinfo>(list);
	}

	/**
	 * 将用户状态修改为false(冻结)
	 * 
	 * @param userid
	 */
	public void moduserstatustofalse(Userinfo user) {
		um.updateByPrimaryKeySelective(new Userinfo(user.getUserid(), null, null, null, null, null, null, null, null,
				null, null, null, null, "false"));

	}
	
	/**
	 * 将用户状态改为true(可用)
	 * @param user
	 */
	public void moduserstatustotrue(Userinfo user) {
		um.updateByPrimaryKeySelective(new Userinfo(user.getUserid(), null, null, null, null, null, null, null, null,
				null, null, null, null, "true"));
	}
	

}
